//
//  CYMyRelease.m
//  CreditAddressBook
//
//  Created by Lee on 15/7/31.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYMyRelease.h"
#import "CYCreditTool.h"
#import "CYUser.h"
#import "CYLoginHttpTool.h"
#import "CYEscape.h"
#import "CYMyReleaseCell.h"
#import "MBProgressHUD+MJ.h"
#import "CYReleaseVC.h"
#import "CYPersonalDetailsVController.h"
#import "CYJumpsTool.h"
#import "CYMyReleaseDetail.h"
#import "CYBackBarButtonItem.h"
#import "CYTool.h"
#import "UIViewController+LEBackButtonHandler.h"

@interface CYMyRelease ()<UITableViewDelegate, UITableViewDataSource, CYMyReleaseDetailDelegate>

@property (weak, nonatomic) IBOutlet UITableView *releaseTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@property (weak, nonatomic) IBOutlet UIView *hintView;

@property (strong, nonatomic) NSArray *defrs;
@end

@implementation CYMyRelease

- (NSArray *)defrs {
    if (!_defrs) {
        _defrs = [NSArray array];
    }
    return _defrs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setExtraCellLineHidden];
    
    [self loadData];
   
//    [self setupBackBtn];
}

- (void)setupBackBtn {
    //自定义返回
    CYBackBarButtonItem *backItem = [[CYBackBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"black-50"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)back {
    NSArray *controllers = self.navigationController.viewControllers;
    NSUInteger index = [controllers indexOfObject:self];
    switch (self.backStyle) {
        case CYBackStyleInterval:
            [self.navigationController popToViewController:controllers[index - CYBackStyleInterval] animated:YES];
            break;
            
        case CYBackStyleDouble:
            [self.navigationController popToViewController:controllers[index - CYBackStyleDouble] animated:YES];
            break;
            
        case CYBackStyleTreble:
            [self.navigationController popToViewController:controllers[index - CYBackStyleTreble] animated:YES];
            break;
            
        default:
            [self.navigationController popViewControllerAnimated:YES];
            break;
    }
}

- (BOOL)navigationShouldPopOnBackButton {
    NSArray *controllers = self.navigationController.viewControllers;
    NSUInteger index = [controllers indexOfObject:self];
    switch (self.backStyle) {
        case CYBackStyleInterval:
            [self.navigationController popToViewController:controllers[index - CYBackStyleInterval] animated:YES];
            break;
            
        case CYBackStyleDouble:
            [self.navigationController popToViewController:controllers[index - CYBackStyleDouble] animated:YES];
            break;
            
        case CYBackStyleTreble:
            [self.navigationController popToViewController:controllers[index - CYBackStyleTreble] animated:YES];
            break;
            
        default:
            [self.navigationController popViewControllerAnimated:YES];
            break;
    }
    return YES;
}

-(void)loadData {
    self.releaseTableView.hidden = YES;
    CYUser *user = [CYLoginHttpTool userData].user;
    NSDictionary *params = @{@"userId": user.userId, @"token": user.token, @"page": @"0"};
    [CYCreditTool entryuiWithParams:params success:^(NSArray *defrs) {
        self.defrs = defrs;
//        [self.releaseTableView reloadData];
        //以动画效果显示数据
        [CYTool cellAnimationWithTableView:self.releaseTableView];
        
        self.hintView.hidden = YES;
        self.releaseTableView.hidden = NO;
    } failure:^(NSError *error) {
        self.hintView.hidden = YES;
        switch (error.code) {
            case CYErrorCodeNOData:
            [MBProgressHUD showError:@"没有数据"];
            break;
            
            default:
            break;
        }
    }];
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.defrs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    CYMyReleaseCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[CYMyReleaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.record = self.defrs[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CYMyReleaseDetail *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"CYMyReleaseDetail"];
    CYDefaultRecord *defr = self.defrs[indexPath.row];
    detail.record = defr;
    detail.delegate = self;
    
    [self.navigationController pushViewController:detail animated:YES];
}

#pragma mark - CYMyReleaseDetailDelegate
- (void)deleteReleaseViewController:(CYMyReleaseDetail *)controller {
    [self loadData];
}

#pragma mark -
- (IBAction)releaseClick:(UIBarButtonItem *)sender {
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user) {
        if (CYStringEqualTo(user.certificateExamineStatusText, @"审核通过")) {
            CYReleaseVC *release = [self.storyboard instantiateViewControllerWithIdentifier:@"CYReleaseVC"];
            release.pushStyle = CYPushStyleFromSetting;
            [self.navigationController pushViewController:release animated:YES];
        } else {
            CYAlertView(@"您无权进行该操作，请先进行审核");
            CYPersonalDetailsVController *personDetail = [self.storyboard instantiateViewControllerWithIdentifier:@"CYPersonalDetailsVController"];
            [self.navigationController pushViewController:personDetail animated:YES];
        }
    } else {
        [CYJumpsTool jumpToLogin:self];
    }
    
}

- (void)setExtraCellLineHidden {
    UIView *view =[[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [self.releaseTableView setTableFooterView:view];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([sender isKindOfClass:[UITableViewCell class]]) {
        NSIndexPath *index = [self.releaseTableView indexPathForCell:sender];
        CYDefaultRecord *defr = self.defrs[index.row];
        CYMyReleaseDetail *controller = segue.destinationViewController;
        controller.delegate = self;
        controller.record = defr;
    }
}

@end
